### This file creates taking sides (congruence) and responsiveness plots
rm(list=ls(all=TRUE))
library(ggplot2)
library(gridExtra)
require(fBasics)
library(arm)
library(stringr)
library(car)
library(abind)
library(readstata13)
library(reshape2)
library(plyr)
library(data.table)


setwd("C:/Users/adamz/Dropbox/RSF Ineqaulity Project/Replication File")
setwd()


ggtheme_personalized <- theme(panel.background = element_blank(),
		panel.grid = element_blank(),
		axis.ticks = element_blank(),
		axis.title.x = element_text(color = "black" , size = 12, face="bold"),
		axis.title.y = element_text(color = "black" , size = 12, face="bold"),
		axis.text.y = element_text(size = 8, colour = "black"),
		axis.text.x = element_text(size = 8, colour = "black"),
		panel.border = element_blank(),
		legend.key = element_rect(colour = "white",fill = "white"),
		legend.text = element_text(size=10,color="black"),
		legend.title = element_text(size = 11, face = "bold", colour = "#535353"),
		strip.text.x = element_text(size = 11, face = "bold", colour = "#535353"),
		strip.background = element_rect(colour = "white", fill = "white"),
		legend.position="none",
		plot.margin = unit(c(0.5,0.5,0.5,0.5), "cm"))


###########################################################################
#################     Create DF of opinion and votes     ##################
###########################################################################

votes <- read.csv(file = "Formatted Data\\roll_call_votes_final.csv" , stringsAsFactors=FALSE)
opinion<- read.csv(file = "Formatted Data\\opinion_estimates_with_nationalQ1-Q5.csv" , stringsAsFactors =FALSE)
opinion <- subset(opinion , issue!="Partial Birth Abortion (2006)") ### Same vote as 08 question

df <- merge(opinion , votes , by = c("state","issue"))
df <- subset(df , vote!=9)
### Convert Bernie, Angus, Jeffords, and Lieberman to Dem
df$party[df$party%in%c("I","ID")] <- "D"

###########################################################################
###############                      Table 4               ################
###########################################################################
ggdata <- df
ggdata$Partisan <-ggdata$OutPartisan <- NA
ggdata$Partisan[ggdata$party=="D"] <- ggdata$Dem[ggdata$party=="D"]
ggdata$Partisan[ggdata$party=="R"] <- ggdata$Rep[ggdata$party=="R"]
ggdata$OutPartisan[ggdata$party=="D"] <- ggdata$Rep[ggdata$party=="D"]
ggdata$OutPartisan[ggdata$party=="R"] <- ggdata$Dem[ggdata$party=="R"]
ggdata$PartisanQ1 <- ggdata$PartisanQ5 <- NA
ggdata$PartisanQ1[ggdata$party=="D"] <- ggdata$Q1D[ggdata$party=="D"]
ggdata$PartisanQ5[ggdata$party=="D"] <- ggdata$Q5D[ggdata$party=="D"]
ggdata$PartisanQ1[ggdata$party=="R"] <- ggdata$Q1R[ggdata$party=="R"]
ggdata$PartisanQ5[ggdata$party=="R"] <- ggdata$Q5R[ggdata$party=="R"]

ggdata0 <- ddply(ggdata , .(party) , function(x) out = data.frame(
  median = mean(x$SW > 0.5 & x$vote == 1 | x$SW < 0.5 & x$vote == 0 ),
  poor = mean(x$Q1 > 0.5 & x$vote == 1 | x$Q1 < 0.5 & x$vote == 0 ),
  rich = mean(x$Q5 > 0.5 & x$vote == 1 | x$Q5 < 0.5 & x$vote == 0 ),
  partisan = mean(x$Partisan > 0.5 & x$vote == 1 | x$Partisan < 0.5 & x$vote == 0 ),
  outpartisan = mean(x$OutPartisan > 0.5 & x$vote == 1 | x$OutPartisan < 0.5 & x$vote == 0 ),
  richpartisan = mean(x$PartisanQ5 > 0.5 & x$vote == 1 | x$PartisanQ5 < 0.5 & x$vote == 0 ),
  poorpartisan = mean(x$PartisanQ1 > 0.5 & x$vote == 1 | x$PartisanQ1 < 0.5 & x$vote == 0 ),
  rich.disagree = mean(x$Q5[x$Q1 < 0.5 & x$Q5 > 0.5 | x$Q1 > 0.5 & x$Q5 < 0.5 ] > 0.5 &
                         x$vote[x$Q1 < 0.5 & x$Q5 > 0.5 | x$Q1 > 0.5 & x$Q5 < 0.5 ] == 1 |
                         x$Q5[x$Q1 < 0.5 & x$Q5 > 0.5 | x$Q1 > 0.5 & x$Q5 < 0.5 ] < 0.5 &
                         x$vote[x$Q1 < 0.5 & x$Q5 > 0.5 | x$Q1 > 0.5 & x$Q5 < 0.5 ] == 0 ),
  partisan.disagree = mean(x$Partisan[x$Partisan < 0.5 & x$OutPartisan > 0.5 | x$Partisan > 0.5 & x$OutPartisan < 0.5 ] > 0.5 &
                             x$vote[x$Partisan < 0.5 & x$OutPartisan > 0.5 | x$Partisan > 0.5 & x$OutPartisan < 0.5 ]  == 1 |
                             x$Partisan[x$Partisan < 0.5 & x$OutPartisan > 0.5 | x$Partisan > 0.5 & x$OutPartisan < 0.5 ] < 0.5 &
                             x$vote[x$Partisan < 0.5 & x$OutPartisan > 0.5 | x$Partisan > 0.5 & x$OutPartisan < 0.5 ]  == 0 )))

ggdata0 <- rbind.data.frame(ggdata0 ,
with(ggdata ,
data.frame(party = "All",
  median = mean(SW > 0.5 & vote == 1 | SW < 0.5 & vote == 0 ),
  poor = mean(Q1 > 0.5 & vote == 1 | Q1 < 0.5 & vote == 0 ),
  rich = mean(Q5 > 0.5 & vote == 1 | Q5 < 0.5 & vote == 0 ),
  partisan = mean(Partisan > 0.5 & vote == 1 | Partisan < 0.5 & vote == 0 ),
  outpartisan = mean(OutPartisan > 0.5 & vote == 1 | OutPartisan < 0.5 & vote == 0 ),
  richpartisan = mean(PartisanQ5 > 0.5 & vote == 1 | PartisanQ5 < 0.5 & vote == 0 ),
  poorpartisan = mean(PartisanQ1 > 0.5 & vote == 1 | PartisanQ1 < 0.5 & vote == 0 ),
  rich.disagree = mean(Q5[Q1 < 0.5 & Q5 > 0.5 | Q1 > 0.5 & Q5 < 0.5 ] > 0.5 &
                         vote[Q1 < 0.5 & Q5 > 0.5 | Q1 > 0.5 & Q5 < 0.5 ] == 1 |
                         Q5[Q1 < 0.5 & Q5 > 0.5 | Q1 > 0.5 & Q5 < 0.5 ] < 0.5 &
                         vote[Q1 < 0.5 & Q5 > 0.5 | Q1 > 0.5 & Q5 < 0.5 ] == 0 ),
  partisan.disagree = mean(Partisan[Partisan < 0.5 & OutPartisan > 0.5 | Partisan > 0.5 & OutPartisan < 0.5 ] > 0.5 &
                             vote[Partisan < 0.5 & OutPartisan > 0.5 | Partisan > 0.5 & OutPartisan < 0.5 ]  == 1 |
                             Partisan[Partisan < 0.5 & OutPartisan > 0.5 | Partisan > 0.5 & OutPartisan < 0.5 ] < 0.5 &
                             vote[Partisan < 0.5 & OutPartisan > 0.5 | Partisan > 0.5 & OutPartisan < 0.5 ]  == 0 ))
)
)
ggdata0

###########################################################################
###############                   Figure 8                 ################
###########################################################################

ggdata <- ddply(df , .(senator,party) , function(x) out = data.frame(
  high = mean(x$Q5 > 0.5 & x$vote == 1 | x$Q5 < 0.5 & x$vote == 0 ),
  low = mean(x$Q1 > 0.5 & x$vote == 1 | x$Q1 < 0.5 & x$vote == 0 ),
  N = as.numeric(length(x$Q5))
))

pdf("Figures for Paper/Figure_8a_congruence_by_senator.pdf",height=6,width=6,onefile=FALSE)
set.seed(1234)
ggplot(subset(ggdata , N>0 ), aes(y=high, x=low, group = party)) +
  geom_segment(aes(x = 0, y = 0 , xend = 1 , yend = 1), colour = "black", linetype=4,size = .5) +
  geom_point(aes(colour = factor(party), shape = factor(party), size = sqrt(N)/pi), alpha = 0.75) +
  scale_x_continuous(name="\nCongruence with Low-Income Constituents\n", limits=c(-.02, 1.05), labels = c("0%","50%","100%"), breaks = c(0,0.5,1)) +
  scale_y_continuous(name="\nCongruence with High-Income Constituents\n", limits=c(-.02, 1.0), labels = c("0%","50%","100%"),breaks = c(0,0.5,1)) +
  ggtheme_personalized +
  theme(plot.margin = unit(c(0.,0.,0.,0.), "cm"),
        panel.grid.major.y=element_blank(),
        legend.title=element_blank(),
        legend.position = "none",
        axis.text.x=element_text(size=14,color="black",vjust=0.5, margin=margin(-15,0,0,0)),
        axis.text.y=element_text(size=14,color="black",hjust=0.5, margin=margin(-15,0,0,0)),
        axis.title.x=element_text(size=16,color="black"),
        axis.title.y=element_text(size=16,color="black")) +
  guides(size="none") +
  scale_colour_manual(values = c("#000069","#b10f2f")) +
  scale_shape_manual(values = c(19,2)) +
  annotate("segment" , x = median(subset(ggdata , N> 0 & party=="D")$low) , xend = median(subset(ggdata , N> 0 & party=="D")$low),
           y = -0.02 , yend = 0.02 , colour = "#000069")  +
  annotate("text" , x = median(subset(ggdata , N> 0 & party=="D")$low) ,
           y = 0.02 , colour = "#000069", label = "DEM MEDIAN",angle = 90 , size = 5,hjust = 0,vjust = 0.3, fontface =2)  +
  annotate("segment" , x = median(subset(ggdata , N> 0 & party=="R")$low) , xend = median(subset(ggdata , N> 0 & party=="R")$low),
           y = -0.02 , yend = 0.02 , colour = "#b10f2f")  +
  annotate("text" , x = median(subset(ggdata , N> 0 & party=="R")$low) ,
           y = 0.02 , colour = "#b10f2f", label = "REP MEDIAN",angle = 90 , size = 5,hjust = 0,vjust = 0.3, fontface =2)  +
  annotate("segment" , y = median(subset(ggdata , N> 0 & party=="D")$high) , yend = median(subset(ggdata , N> 0 & party=="D")$high),
           x = -0.02 , xend = 0.02 , colour = "#000069")  +
  annotate("text" , y = median(subset(ggdata , N> 0 & party=="D")$high) ,
           x = 0.02 , colour = "#000069", label = "DEM MEDIAN",angle = 0 , size = 5,hjust = 0,vjust = 0.3, fontface =2)  +
  annotate("segment" , y = median(subset(ggdata , N> 0 & party=="R")$high) , yend = median(subset(ggdata , N> 0 & party=="R")$high),
           x = -0.02 , xend = 0.02 , colour = "#b10f2f")  +
  annotate("text" , y = median(subset(ggdata , N> 0 & party=="R")$high) ,
           x = 0.02 , colour = "#b10f2f", label = "REP MEDIAN",angle = 0 , size = 5,hjust = 0,vjust = 0.3, fontface =2)  +
  annotate("segment" , y = 0 , yend = 1,
           x = 0, xend = 0 , colour = "black", size = .5)  +
  annotate("segment" , y = 0 , yend = 0,
           x = 0, xend = 1 , colour = "black", size = .5)  +
  theme(legend.key = element_rect(colour = "white",fill = "white"))
dev.off()


ggdata <- ddply(df , .(senator,party) , function(x) out = data.frame(
	Dem = mean(x$Dem > 0.5 & x$vote == 1 | x$Dem < 0.5 & x$vote == 0 ),
	Rep = mean(x$Rep > 0.5 & x$vote == 1 | x$Rep < 0.5 & x$vote == 0 ),
	N = as.numeric(length(x$Q5))
))

pdf("Figures for Paper/Figure_8b_congruence_by_senator_partisan.pdf",height=6,width=6,onefile=FALSE)
set.seed(1234)
ggplot(subset(ggdata , N>0 ), aes(y=Rep, x=Dem, group = party)) +
  geom_segment(aes(x = 0, y = 0 , xend = 1 , yend = 1), colour = "black", linetype=4,size = .5) +
  geom_point(aes(colour = factor(party), shape = factor(party), size = sqrt(N)/pi), alpha = 0.75) +
  scale_x_continuous(name="\nCongruence with Democratic Constituents\n", limits=c(-.02, 1.05), labels = c("0%","50%","100%"),breaks = c(0,0.5,1)) +
	scale_y_continuous(name="\nCongruence with Republican Constituents\n", limits=c(-.02, 1.0), labels = c("0%","50%","100%"), breaks = c(0,0.5,1)) +
	ggtheme_personalized +
  theme(plot.margin = unit(c(0.,0.,0.,0.), "cm"),
        panel.grid.major.y=element_blank(),
        legend.title=element_blank(),
        legend.position = "none",
        axis.text.x=element_text(size=14,color="black",vjust=0.5, margin=margin(-15,0,0,0)),
        axis.text.y=element_text(size=14,color="black",hjust=0.5, margin=margin(-15,0,0,0)),
        axis.title.x=element_text(size=16,color="black"),
        axis.title.y=element_text(size=16,color="black")) +
  guides(size="none") +
  scale_colour_manual(values = c("#000069","#b10f2f")) +
  scale_shape_manual(values = c(19,2)) +
  annotate("segment" , x = median(subset(ggdata , N> 0 & party=="D")$Dem) , xend = median(subset(ggdata , N> 0 & party=="D")$Dem),
		y = -0.02 , yend = 0.02 , colour = "#000069")  +
	annotate("text" , x = median(subset(ggdata , N> 0 & party=="D")$Dem) ,
		y = 0.02 , colour = "#000069", label = "DEM MEDIAN",angle = 90 , size = 5,hjust = 0,vjust = 0.3, fontface =2)  +
	annotate("segment" , x = median(subset(ggdata , N> 0 & party=="R")$Dem) , xend = median(subset(ggdata , N> 0 & party=="R")$Dem),
		y = -0.02 , yend = 0.02 , colour = "#b10f2f")  +
	annotate("text" , x = median(subset(ggdata , N> 0 & party=="R")$Dem) ,
		y = 0.02 , colour = "#b10f2f", label = "REP MEDIAN",angle = 90 , size = 5,hjust = 0,vjust = 0.3, fontface =2)  +
	annotate("segment" , y = median(subset(ggdata , N> 0 & party=="D")$Rep) , yend = median(subset(ggdata , N> 0 & party=="D")$Rep),
		x = -0.02 , xend = 0.02 , colour = "#000069")  +
	annotate("text" , y = median(subset(ggdata , N> 0 & party=="D")$Rep) ,
		x = 0.02 , colour = "#000069", label = "DEM MEDIAN",angle = 0 , size = 5,hjust = 0,vjust = 0.3, fontface =2)  +
	annotate("segment" , y = median(subset(ggdata , N> 0 & party=="R")$Rep) , yend = median(subset(ggdata , N> 0 & party=="R")$Rep),
		x = -0.02 , xend = 0.02 , colour = "#b10f2f")  +
	annotate("text" , y = median(subset(ggdata , N> 0 & party=="R")$Rep) ,
		x = 0.02 , colour = "#b10f2f", label = "REP MEDIAN",angle = 0 , size = 5,hjust = 0,vjust = 0.3, fontface =2)  +
	annotate("segment" , y = 0 , yend = 1,
		x = 0, xend = 0 , colour = "black", size = .5)  +
	annotate("segment" , y = 0 , yend = 0,
		x = 0, xend = 1 , colour = "black", size = .5)  +
	theme(legend.key = element_rect(colour = "white",fill = "white"))
dev.off()


ggdata <- ddply(df , .(senator,party) , function(x) out = data.frame(
	Dem = mean(x$Dem > 0.5 & x$vote == 1 | x$Dem < 0.5 & x$vote == 0 ),
	Rep = mean(x$Rep > 0.5 & x$vote == 1 | x$Rep < 0.5 & x$vote == 0 ),
	SW = mean(x$SW > 0.5 & x$vote == 1 | x$SW < 0.5 & x$vote == 0 ),
	N = as.numeric(length(x$Q5))
))
ggdata$Partisan <- NA
ggdata$Partisan[ggdata$party=="D"] <- ggdata$Dem[ggdata$party=="D"]
ggdata$Partisan[ggdata$party=="R"] <- ggdata$Rep[ggdata$party=="R"]

mean(ggdata$SW[ggdata$party=="D"])
mean(ggdata$SW[ggdata$party=="R"])
median(ggdata$SW[ggdata$party=="D"])
median(ggdata$SW[ggdata$party=="R"])

pdf("Figures for Paper/Figure_8c_congruence_by_senator_state.pdf",height=6,width=6,onefile=FALSE)
set.seed(1234)
ggplot(subset(ggdata , N>0 ), aes(y=Partisan, x=SW, group = party)) +
  geom_segment(aes(x = 0, y = 0 , xend = 1 , yend = 1), colour = "black", linetype=4,size = .5) +
  geom_point(aes(colour = factor(party), shape = factor(party), size = sqrt(N)/pi), alpha = 0.75) +
  scale_x_continuous(name="\nCongruence with Statewide Constituents\n", limits=c(-.02, 1.05), labels = c("0%","50%","100%"),breaks = c(0,0.5,1)) +
	scale_y_continuous(name="\nCongruence with Partisan Constituents\n", limits=c(-.02, 1.0), labels = c("0%","50%","100%"),breaks = c(0,0.5,1)) +
	ggtheme_personalized +
  theme(plot.margin = unit(c(0.,0.,0.,0.), "cm"),
        panel.grid.major.y=element_blank(),
        legend.title=element_blank(),
        legend.position = "none",
        axis.text.x=element_text(size=14,color="black",vjust=0.5, margin=margin(-15,0,0,0)),
        axis.text.y=element_text(size=14,color="black",hjust=0.5, margin=margin(-15,0,0,0)),
        axis.title.x=element_text(size=16,color="black"),
        axis.title.y=element_text(size=16,color="black")) +
  guides(size="none") +
  scale_colour_manual(values = c("#000069","#b10f2f")) +
  scale_shape_manual(values = c(19,2)) +
  annotate("segment" , x = median(subset(ggdata , N> 0 & party=="D")$SW) , xend = median(subset(ggdata , N> 0 & party=="D")$SW),
		y = -0.02 , yend = 0.02 , colour = "#000069")  +
	annotate("text" , x = median(subset(ggdata , N> 0 & party=="D")$SW) ,
		y = 0.02 , colour = "#000069", label = "DEM MEDIAN",angle = 90 , size = 5,hjust = 0,vjust = 0.3, fontface =2)+
	annotate("segment" , x = median(subset(ggdata , N> 0 & party=="R")$SW) , xend = median(subset(ggdata , N> 0 & party=="R")$SW),
		y = -0.02 , yend = 0.02 , colour = "#b10f2f")  +
	annotate("text" , x = median(subset(ggdata , N> 0 & party=="R")$SW) ,
		y = 0.02 , colour = "#b10f2f", label = "REP MEDIAN",angle = 90 , size = 5,hjust = 0,vjust = 0.3, fontface =2)+
	annotate("segment" , y = median(subset(ggdata , N> 0 & party=="D")$Partisan) , yend = median(subset(ggdata , N> 0 & party=="D")$Partisan),
		x = -0.02 , xend = 0.02 , colour = "#000069")  +
	annotate("text" , y = median(subset(ggdata , N> 0 & party=="D")$Partisan) ,
		x = 0.02 , colour = "#000069", label = "DEM MEDIAN",angle = 0 , size = 5,hjust = 0,vjust = 0.3, fontface =2)+
	annotate("segment" , y = median(subset(ggdata , N> 0 & party=="R")$Partisan) , yend = median(subset(ggdata , N> 0 & party=="R")$Partisan),
		x = -0.02 , xend = 0.02 , colour = "#b10f2f")  +
	annotate("text" , y = median(subset(ggdata , N> 0 & party=="R")$Partisan) ,
		x = 0.02 , colour = "#b10f2f", label = "REP MEDIAN",angle = 0 , size = 5,hjust = 0,vjust = 0.3, fontface =2)+
	annotate("segment" , y = 0 , yend = 1,
		x = 0, xend = 0 , colour = "black", size = .5)  +
	annotate("segment" , y = 0 , yend = 0,
		x = 0, xend = 1 , colour = "black", size = .5)  +
	theme(legend.key = element_rect(colour = "white",fill = "white"))
dev.off()


ggdata <- ddply(df , .(senator,party) , function(x) out = data.frame(
	D1 = mean(x$Q1D > 0.5 & x$vote == 1 | x$Q1D < 0.5 & x$vote == 0 ),
	D5 = mean(x$Q5D > 0.5 & x$vote == 1 | x$Q5D < 0.5 & x$vote == 0 ),
	R1 = mean(x$Q1R > 0.5 & x$vote == 1 | x$Q1R < 0.5 & x$vote == 0 ),
	R5 = mean(x$Q5R > 0.5 & x$vote == 1 | x$Q5R < 0.5 & x$vote == 0 ),
	N = as.numeric(length(x$Q5))
))
ggdata$Partisan1 <- ggdata$Partisan5 <- NA
ggdata$Partisan1[ggdata$party=="D"] <- ggdata$D1[ggdata$party=="D"]
ggdata$Partisan5[ggdata$party=="D"] <- ggdata$D5[ggdata$party=="D"]
ggdata$Partisan1[ggdata$party=="R"] <- ggdata$R1[ggdata$party=="R"]
ggdata$Partisan5[ggdata$party=="R"] <- ggdata$R5[ggdata$party=="R"]

pdf("Figures for Paper/Figure_8d_congruence_by_senator_ownparty_high_low.pdf",height=6,width=6,onefile=FALSE)
set.seed(1234)
ggplot(subset(ggdata , N>0 ), aes(y=Partisan5, x=Partisan1, group = party)) +
	geom_segment(aes(x = 0, y = 0 , xend = 1 , yend = 1), colour = "black", linetype=4,size = .5) +
	geom_point(aes(colour = factor(party), shape = factor(party), size = sqrt(N)/pi), alpha = 0.75) +
	scale_x_continuous(name="\nCongruence with Low-Income In-partisans\n", limits=c(-.02, 1.05), labels = c("0%","50%","100%"),breaks = c(0,0.5,1)) +
	scale_y_continuous(name="\nCongruence with High-Income In-partisans\n", limits=c(-.02, 1.0), labels = c("0%","50%","100%"),breaks = c(0,0.5,1)) +
	ggtheme_personalized +
  theme(plot.margin = unit(c(0.,0.,0.,0.), "cm"),
        panel.grid.major.y=element_blank(),
        legend.title=element_blank(),
        legend.position = "none",
        axis.text.x=element_text(size=14,color="black",vjust=0.5, margin=margin(-15,0,0,0)),
        axis.text.y=element_text(size=14,color="black",hjust=0.5, margin=margin(-15,0,0,0)),
        axis.title.x=element_text(size=16,color="black"),
        axis.title.y=element_text(size=16,color="black")) +
  guides(size="none") +
  scale_colour_manual(values = c("#000069","#b10f2f")) +
  scale_shape_manual(values = c(19,2)) +
  annotate("segment" , x = median(subset(ggdata , N> 0 & party=="D")$Partisan1) , xend = median(subset(ggdata , N> 0 & party=="D")$Partisan1),
		y = -0.02 , yend = 0.02 , colour = "#000069")  +
	annotate("text" , x = median(subset(ggdata , N> 0 & party=="D")$Partisan1) ,
		y = 0.02 , colour = "#000069", label = "DEM MEDIAN",angle = 90 , size = 5,hjust = 0,vjust = 0.3, fontface =2)+
	annotate("segment" , x = median(subset(ggdata , N> 0 & party=="R")$Partisan1) , xend = median(subset(ggdata , N> 0 & party=="R")$Partisan1),
		y = -0.02 , yend = 0.02 , colour = "#b10f2f")  +
	annotate("text" , x = median(subset(ggdata , N> 0 & party=="R")$Partisan1) ,
		y = 0.02 , colour = "#b10f2f", label = "REP MEDIAN",angle = 90 , size = 5,hjust = 0,vjust = 0.3, fontface =2)+
	annotate("segment" , y = median(subset(ggdata , N> 0 & party=="D")$Partisan5) , yend = median(subset(ggdata , N> 0 & party=="D")$Partisan5),
		x = -0.02 , xend = 0.02 , colour = "#000069")  +
	annotate("text" , y = median(subset(ggdata , N> 0 & party=="D")$Partisan5) ,
		x = 0.02 , colour = "#000069", label = "DEM MEDIAN",angle = 0 , size = 5,hjust = 0,vjust = 0.3, fontface =2)+
	annotate("segment" , y = median(subset(ggdata , N> 0 & party=="R")$Partisan5) , yend = median(subset(ggdata , N> 0 & party=="R")$Partisan5),
		x = -0.02 , xend = 0.02 , colour = "#b10f2f")  +
	annotate("text" , y = median(subset(ggdata , N> 0 & party=="R")$Partisan5) ,
		x = 0.02 , colour = "#b10f2f", label = "REP MEDIAN",angle = 0 , size = 5,hjust = 0,vjust = 0.3, fontface =2)+
	annotate("segment" , y = 0 , yend = 1,
		x = 0, xend = 0 , colour = "black", size = .5)  +
	annotate("segment" , y = 0 , yend = 0,
		x = 0, xend = 1 , colour = "black", size = .5)  +
	theme(legend.key = element_rect(colour = "white",fill = "white"))
dev.off()


###########################################################################
############          TAKING SIDES (Figures 9 - 15)          ##############
###########################################################################

ggdata <- ddply(subset(df , Q5 > 0.5 & Q1 < 0.5 | Q5 < 0.5 & Q1 > 0.5) , .(senator,party) , function(x) out = data.frame(
	high = mean(x$Q5 > 0.5 & x$vote == 1 | x$Q5 < 0.5 & x$vote == 0 ),
	low = mean(x$Q1 > 0.5 & x$vote == 1 | x$Q1 < 0.5 & x$vote == 0 ),
	N = as.numeric(length(x$Q5))
))
set.seed(1234)

ggdata$high2 <- ggdata$high - runif(nrow(ggdata))/100 + .005
ggdata$high3 <- ggdata$high2
# Hack for displaying above and below line
ggdata$high2[ggdata$party=="D"] <- 2
ggdata$high3[ggdata$party=="R"] <- 2
ggdataR <- subset(ggdata , N > 0 & party=="R")
ggdataD <- subset(ggdata , N > 0 & party=="D")

tempR <- density(ggdataR$high)
tempR <- data.frame(x = tempR$x , y = tempR$y)
tempR$y[tempR$x > 1] <- 0
tempR$y[tempR$x < 0] <- 0
tempD <- density(ggdataD$high)
tempD <- data.frame(x = tempD$x , y = tempD$y)
tempD$y[tempD$x < 0] <- 0
tempD$y[tempD$x > 1] <- 0

pdf("Figures for Paper/Figure_9_congruence_by_senator_when_differing.pdf",height=3,width=10.5,onefile=FALSE)
ggplot(data = subset(ggdata , N > 0)) +
 	geom_point(aes(x = high2 , y=.125*N/25, size = N/15), colour = "#b10f2f",fill = "#b10f2f",pch=24,alpha=0.5) +
 	geom_point(aes(x = high3 , y=-.125*N/25, size = N/15), colour = "#000069",fill = "#000069",pch=25,alpha=0.5) +
	geom_segment(aes(x = .5, y = -.25 , xend = .5 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 0, y = -.25 , xend = 0 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 1, y = -.25 , xend = 1 , yend = .25), colour = "black", size = .5) +
	geom_polygon(data = tempR , aes(x = x , y = y*.3), alpha = 0.15, fill = "#b10f2f") +
	geom_polygon(data = tempD , aes(x = x , y = -y*.3), alpha = 0.15, fill = "#000069") +
	ggtheme_personalized +
	theme(legend.position="right",
		panel.grid.major.y=element_blank(),
		legend.title=element_blank(),
		legend.key = element_rect(colour = "white",fill = "white"),
		legend.text = element_text(size=10,color="grey50"),
		axis.title.y = element_blank(),
		axis.text.y = element_blank(),
		plot.margin = unit(c(0.,0.,0.,0.), "cm"),
		axis.title.x = element_blank(),
		axis.text.x = element_blank()) +
	xlim(c(-.06,1.05)) + ylim(c(-1.3,1.5)) +
	guides(size = "none", alpha="none" , density="none") +
	scale_colour_manual(breaks = c("R","D"), values = c("#000069","#b10f2f"), labels = c("Republican","Democrat")) +
	annotate("text",x = 0, y = -.5 , label = "Votes 100%\n with Poor Constituents", colour = "grey30" , size = 4 , hjust = .5) +
	annotate("text",x = 1, y = -.5 , label = "Votes 100%\n with Rich Constituents", colour = "grey30" , size = 4 , hjust = .5) +
	geom_segment(aes(x = weighted.mean(ggdataR$high,ggdataR$N) , y = 0 , xend = weighted.mean(ggdataR$high,ggdataR$N) , yend = 0.5), colour = "#b10f2f", size = 0.5) +
	geom_segment(aes(x = weighted.mean(ggdataD$high,ggdataD$N), y = -.5 , xend = weighted.mean(ggdataD$high,ggdataD$N) , yend = 0), colour = "#000069", size = 0.5) +
	annotate("text", x = 1.0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*ggdata$high[ggdata$party=="D"])," Votes"), size = 5, colour = "#000069", hjust = 1)+
	annotate("text", x = 0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*(1-ggdata$high[ggdata$party=="D"]))," Votes"), size = 5, colour = "#000069", hjust = 0)+
	annotate("text", x = 1.0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*ggdata$high[ggdata$party=="R"]),"  Votes"), size = 5, colour = "#b10f2f", hjust = 1)+
	annotate("text", x = 0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*(1-ggdata$high[ggdata$party=="R"])),"  Votes"), size =5, colour = "#b10f2f", hjust = 0)+
	annotate("text", x = 0, y = 0, label = "Republicans", size = 4, colour = "#b10f2f", hjust = 1.1 , vjust=-.4)+
	annotate("text", x = 0, y = 0, label = "Democrats", size = 4, colour = "#000069", hjust = 1.1 , vjust=1.2)+
	geom_segment(aes(x = .0, y = 0 , xend = 1.0 , yend = 0), colour = "black", size = 1.5)
dev.off()

ggdata <- ddply(
	subset(df , Dem > 0.5 & Rep < 0.5 | Dem < 0.5 & Rep > 0.5),
	.(senator,party) , function(x) out = data.frame(
		high = mean(x$Rep > 0.5 & x$vote == 1 | x$Rep < 0.5 & x$vote == 0 ),
		N = as.numeric(length(x$Rep))
))

set.seed(1234)
ggdata$high2 <- ggdata$high - runif(nrow(ggdata))/100 + .005 ### Jitter
ggdata$high3 <- ggdata$high2
ggdata$high2[ggdata$party=="D"] <- 2
ggdata$high3[ggdata$party=="R"] <- 2
ggdataR <- subset(ggdata , N > 0 & party=="R")
ggdataD <- subset(ggdata , N > 0 & party=="D")


tempR <- density(ggdataR$high)
tempR <- data.frame(x = tempR$x , y = tempR$y)
tempR$y[tempR$x > 1] <- 0
tempR$y[tempR$x < 0] <- 0
tempD <- density(ggdataD$high)
tempD <- data.frame(x = tempD$x , y = tempD$y)
tempD$y[tempD$x < 0] <- 0
tempD$y[tempD$x > 1] <- 0

pdf("Figures for Paper/Figure_11a_congruence_by_senator_when_differing_parties.pdf",height=3,width=10.5,onefile=FALSE)
ggplot(data = subset(ggdata , N > 0)) +
 	geom_point(aes(x = high2 , y=.125*N/45, size = N/15), colour = "#b10f2f",fill = "#b10f2f",pch=24,alpha=0.5) +
 	geom_point(aes(x = high3 , y=-.125*N/45, size = N/15), colour = "#000069",fill = "#000069",pch=25,alpha=0.5) +
	geom_segment(aes(x = .5, y = -.25 , xend = .5 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 0, y = -.25 , xend = 0 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 1, y = -.25 , xend = 1 , yend = .25), colour = "black", size = .5) +
	geom_polygon(data = tempR , aes(x = x , y = y*.3), alpha = 0.15, fill = "#b10f2f") +
	geom_polygon(data = tempD , aes(x = x , y = -y*.3), alpha = 0.15, fill = "#000069") +
	ggtheme_personalized +
	theme(legend.position="right",
		panel.grid.major.y=element_blank(),
		legend.title=element_blank(),
		legend.key = element_rect(colour = "white",fill = "white"),
		legend.text = element_text(size=10,color="grey50"),
		axis.title.y = element_blank(),
		axis.text.y = element_blank(),
		plot.margin = unit(c(0.,0.,0.,0.), "cm"),
		axis.title.x = element_blank(),
		axis.text.x = element_blank()) +
	xlim(c(-.06,1.05)) + ylim(c(-1.3,1.5)) + guides(size = "none", alpha="none" , density="none") +
	scale_colour_manual(breaks = c("R","D"), values = c("#000069","#b10f2f"), labels = c("Republican","Democrat")) +
	annotate("text",x = 0, y = -.5 , label = "Votes 100%\n with Dem Constituents", colour = "grey30" , size = 4 , hjust = .5) +
	annotate("text",x = 1, y = -.5 , label = "Votes 100%\n with Rep Constituents", colour = "grey30" , size = 4 , hjust = .5) +
	geom_segment(aes(x = weighted.mean(ggdataR$high,ggdataR$N) , y = 0 , xend = weighted.mean(ggdataR$high,ggdataR$N) , yend = 0.5), colour = "#b10f2f", size = 0.5) +
	geom_segment(aes(x = weighted.mean(ggdataD$high,ggdataD$N), y = -.5 , xend = weighted.mean(ggdataD$high,ggdataD$N) , yend = 0), colour = "#000069", size = 0.5) +
	annotate("text", x = 1.0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*ggdata$high[ggdata$party=="D"])," Votes"), size = 5, colour = "#000069", hjust = 1)+
	annotate("text", x = 0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*(1-ggdata$high[ggdata$party=="D"]))," Votes"), size = 5, colour = "#000069", hjust = 0)+
	annotate("text", x = 1.0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*ggdata$high[ggdata$party=="R"]),"  Votes"), size = 5, colour = "#b10f2f", hjust = 1)+
	annotate("text", x = 0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*(1-ggdata$high[ggdata$party=="R"])),"  Votes"), size = 5, colour = "#b10f2f", hjust = 0)+
	annotate("text", x = 0, y = 0, label = "Republicans", size = 4, colour = "#b10f2f", hjust = 1.1 , vjust=-.4)+
	annotate("text", x = 0, y = 0, label = "Democrats", size = 4, colour = "#000069", hjust = 1.1 , vjust=1.2)+
	geom_segment(aes(x = .0, y = 0 , xend = 1.0 , yend = 0), colour = "black", size = 1.5)
dev.off()


ggdata <- df
ggdata$Partisan <- NA
ggdata$Partisan[ggdata$party=="D"] <- ggdata$Dem[ggdata$party=="D"]
ggdata$Partisan[ggdata$party=="R"] <- ggdata$Rep[ggdata$party=="R"]

ggdata <- ddply(
	subset(ggdata , Partisan > 0.5 & SW < 0.5 | Partisan < 0.5 & SW > 0.5),
	.(senator,party) , function(x) out = data.frame(
		high = mean(x$Partisan > 0.5 & x$vote == 1 | x$Partisan < 0.5 & x$vote == 0 ),
		N = as.numeric(length(x$Partisan))
))

set.seed(1234)
ggdata$high2 <- ggdata$high - runif(nrow(ggdata))/100 + .005
ggdata$high3 <- ggdata$high2
ggdata$high2[ggdata$party=="D"] <- 2
ggdata$high3[ggdata$party=="R"] <- 2
ggdataR <- subset(ggdata , N > 0 & party=="R")
ggdataD <- subset(ggdata , N > 0 & party=="D")


tempR <- density(ggdataR$high)
tempR <- data.frame(x = tempR$x , y = tempR$y)
tempR$y[tempR$x > 1] <- 0
tempR$y[tempR$x < 0] <- 0
tempD <- density(ggdataD$high)
tempD <- data.frame(x = tempD$x , y = tempD$y)
tempD$y[tempD$x < 0] <- 0
tempD$y[tempD$x > 1] <- 0


pdf("Figures for Paper/Figure_11b_congruence_by_senator_when_differing_state_party.pdf",height=3,width=10.5,onefile=FALSE)
ggplot(data = subset(ggdata , N > 0)) +
 	geom_point(aes(x = high2 , y=.125*N/35, size = N/15), colour = "#b10f2f",fill = "#b10f2f",pch=24,alpha=0.5) +
 	geom_point(aes(x = high3 , y=-.125*N/35, size = N/15), colour = "#000069",fill = "#000069",pch=25,alpha=0.5) +
	geom_segment(aes(x = .5, y = -.25 , xend = .5 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 0, y = -.25 , xend = 0 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 1, y = -.25 , xend = 1 , yend = .25), colour = "black", size = .5) +
	geom_polygon(data = tempR , aes(x = x , y = y*.3), alpha = 0.15, fill = "#b10f2f") +
	geom_polygon(data = tempD , aes(x = x , y = -y*.3), alpha = 0.15, fill = "#000069") +
	ggtheme_personalized +
	theme(legend.position="right",
		panel.grid.major.y=element_blank(),
		legend.title=element_blank(),
		legend.key = element_rect(colour = "white",fill = "white"),
		legend.text = element_text(size=10,color="grey50"),
		axis.title.y = element_blank(),
		axis.text.y = element_blank(),
		plot.margin = unit(c(0.,0.,0.,0.), "cm"),
		axis.title.x = element_blank(),
		axis.text.x = element_blank()) +
	xlim(c(-.06,1.05)) + ylim(c(-1.3,1.5)) + guides(size = "none", alpha="none" , density="none") +
	scale_colour_manual(breaks = c("R","D"), values = c("#000069","#b10f2f"), labels = c("Republican","Democrat")) +
	annotate("text",x = 0, y = -.5 , label = "Votes 100%\n with Statewide Constituents", colour = "grey30" , size = 4 , hjust = .5) +
	annotate("text",x = 1, y = -.5 , label = "Votes 100%\n with Partisan Constituents", colour = "grey30" , size = 4 , hjust = .5) +
	geom_segment(aes(x = weighted.mean(ggdataR$high,ggdataR$N) , y = 0 , xend = weighted.mean(ggdataR$high,ggdataR$N) , yend = 0.5), colour = "#b10f2f", size = 0.5) +
	geom_segment(aes(x = weighted.mean(ggdataD$high,ggdataD$N), y = -.5 , xend = weighted.mean(ggdataD$high,ggdataD$N) , yend = 0), colour = "#000069", size = 0.5) +
	annotate("text", x = 1.0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*ggdata$high[ggdata$party=="D"])," Votes"), size = 5, colour = "#000069", hjust = 1)+
	annotate("text", x = 0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*(1-ggdata$high[ggdata$party=="D"]))," Votes"), size = 5, colour = "#000069", hjust = 0)+
	annotate("text", x = 1.0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*ggdata$high[ggdata$party=="R"]),"  Votes"), size = 5, colour = "#b10f2f", hjust = 1)+
	annotate("text", x = 0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*(1-ggdata$high[ggdata$party=="R"])),"  Votes"), size = 5, colour = "#b10f2f", hjust = 0)+
	annotate("text", x = 0, y = 0, label = "Republicans", size = 4, colour = "#b10f2f", hjust = 1.1 , vjust=-.4)+
	annotate("text", x = 0, y = 0, label = "Democrats", size = 4, colour = "#000069", hjust = 1.1 , vjust=1.2)+
	geom_segment(aes(x = .0, y = 0 , xend = 1.0 , yend = 0), colour = "black", size = 1.5)
dev.off()


ggdata <- df
ggdata$Partisan <- NA
ggdata$Partisan[ggdata$party=="D"] <- ggdata$Dem[ggdata$party=="D"]
ggdata$Partisan[ggdata$party=="R"] <- ggdata$Rep[ggdata$party=="R"]

ggdata <- ddply(
	subset(ggdata , Q5 > 0.5 & SW < 0.5 | Q5 < 0.5 & SW > 0.5),
	.(senator,party) , function(x) out = data.frame(
		high = mean(x$Q5 > 0.5 & x$vote == 1 | x$Q5 < 0.5 & x$vote == 0 ),
		N = as.numeric(length(x$Q5))
))

set.seed(1234)
ggdata$high2 <- ggdata$high - runif(nrow(ggdata))/100 + .005
ggdata$high3 <- ggdata$high2
ggdata$high2[ggdata$party=="D"] <- 2
ggdata$high3[ggdata$party=="R"] <- 2
ggdataR <- subset(ggdata , N > 0 & party=="R")
ggdataD <- subset(ggdata , N > 0 & party=="D")

tempR <- density(ggdataR$high)
tempR <- data.frame(x = tempR$x , y = tempR$y)
tempR$y[tempR$x > 1] <- 0
tempR$y[tempR$x < 0] <- 0
tempD <- density(ggdataD$high)
tempD <- data.frame(x = tempD$x , y = tempD$y)
tempD$y[tempD$x < 0] <- 0
tempD$y[tempD$x > 1] <- 0

pdf("Figures for Paper/Figure_10a_congruence_by_senator_when_differing_state_wealthy.pdf",height=3,width=10.5,onefile=FALSE)
ggplot(data = subset(ggdata , N > 0)) +
 	geom_point(aes(x = high2 , y=.125*N/15, size = N/15), colour = "#b10f2f",fill = "#b10f2f",pch=24,alpha=0.5) +
 	geom_point(aes(x = high3 , y=-.125*N/15, size = N/15), colour = "#000069",fill = "#000069",pch=25,alpha=0.5) +
	geom_segment(aes(x = .5, y = -.25 , xend = .5 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 0, y = -.25 , xend = 0 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 1, y = -.25 , xend = 1 , yend = .25), colour = "black", size = .5) +
	geom_polygon(data = tempR , aes(x = x , y = y*.3), alpha = 0.15, fill = "#b10f2f") +
	geom_polygon(data = tempD , aes(x = x , y = -y*.3), alpha = 0.15, fill = "#000069") +
	theme(axis.title.x=element_blank(),
		axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.25))+
	ggtheme_personalized +
	theme(legend.position="right",
		panel.grid.major.y=element_blank(),
		legend.title=element_blank(),
		legend.key = element_rect(colour = "white",fill = "white"),
		legend.text = element_text(size=10,color="grey50"),
		axis.title.y = element_blank(),
		axis.text.y = element_blank(),
		plot.margin = unit(c(0.,0.,0.,0.), "cm"),
		axis.title.x = element_blank(),
		axis.text.x = element_blank()) +
	xlim(c(-.06,1.05)) + ylim(c(-1.3,1.5)) + guides(size = "none", alpha="none" , density="none") +
	scale_colour_manual(breaks = c("R","D"), values = c("#000069","#b10f2f"), labels = c("Republican","Democrat")) +
	annotate("text",x = 0, y = -.5 , label = "Votes 100%\n with Statewide Constituents", colour = "grey30" , size = 4 , hjust = .5) +
	annotate("text",x = 1, y = -.5 , label = "Votes 100%\n with Rich Constituents", colour = "grey30" , size = 4 , hjust = .5) +
	geom_segment(aes(x = weighted.mean(ggdataR$high,ggdataR$N) , y = 0 , xend = weighted.mean(ggdataR$high,ggdataR$N) , yend = 0.5), colour = "#b10f2f", size = 0.5) +
	geom_segment(aes(x = weighted.mean(ggdataD$high,ggdataD$N), y = -.5 , xend = weighted.mean(ggdataD$high,ggdataD$N) , yend = 0), colour = "#000069", size = 0.5) +
	annotate("text", x = 1.0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*ggdata$high[ggdata$party=="D"])," Votes"), size = 5, colour = "#000069", hjust = 1)+
	annotate("text", x = 0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*(1-ggdata$high[ggdata$party=="D"]))," Votes"), size = 5, colour = "#000069", hjust = 0)+
	annotate("text", x = 1.0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*ggdata$high[ggdata$party=="R"]),"  Votes"), size = 5, colour = "#b10f2f", hjust = 1)+
	annotate("text", x = 0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*(1-ggdata$high[ggdata$party=="R"])),"  Votes"), size = 5, colour = "#b10f2f", hjust = 0)+
	annotate("text", x = 0, y = 0, label = "Republicans", size = 4, colour = "#b10f2f", hjust = 1.1 , vjust=-.4)+
	annotate("text", x = 0, y = 0, label = "Democrats", size = 4, colour = "#000069", hjust = 1.1 , vjust=1.2)+
	geom_segment(aes(x = .0, y = 0 , xend = 1.0 , yend = 0), colour = "black", size = 1.5)
dev.off()


ggdata <- df
ggdata$Partisan <- NA
ggdata$Partisan[ggdata$party=="D"] <- ggdata$Dem[ggdata$party=="D"]
ggdata$Partisan[ggdata$party=="R"] <- ggdata$Rep[ggdata$party=="R"]

ggdata <- ddply(
	subset(ggdata , Q1 > 0.5 & SW < 0.5 | Q1 < 0.5 & SW > 0.5),
	.(senator,party) , function(x) out = data.frame(
		high = mean(x$Q1 > 0.5 & x$vote == 1 | x$Q1 < 0.5 & x$vote == 0 ),
		N = as.numeric(length(x$Q5))
))

set.seed(1234)
ggdata$high2 <- ggdata$high - runif(nrow(ggdata))/100 + .005
ggdata$high3 <- ggdata$high2
ggdata$high2[ggdata$party=="D"] <- 2
ggdata$high3[ggdata$party=="R"] <- 2
ggdataR <- subset(ggdata , N > 0 & party=="R")
ggdataD <- subset(ggdata , N > 0 & party=="D")

tempR <- density(ggdataR$high)
tempR <- data.frame(x = tempR$x , y = tempR$y)
tempR$y[tempR$x > 1] <- 0
tempR$y[tempR$x < 0] <- 0
tempD <- density(ggdataD$high)
tempD <- data.frame(x = tempD$x , y = tempD$y)
tempD$y[tempD$x < 0] <- 0
tempD$y[tempD$x > 1] <- 0


pdf("Figures for Paper/Figure_10b_congruence_by_senator_when_differing_state_poor.pdf",height=3,width=10.5,onefile=FALSE)
ggplot(data = subset(ggdata , N > 0)) +
 	geom_point(aes(x = high2 , y=.125*N/15, size = N/15), colour = "#b10f2f",fill = "#b10f2f",pch=24,alpha=0.5) +
 	geom_point(aes(x = high3 , y=-.125*N/15, size = N/15), colour = "#000069",fill = "#000069",pch=25,alpha=0.5) +
	geom_segment(aes(x = .5, y = -.25 , xend = .5 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 0, y = -.25 , xend = 0 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 1, y = -.25 , xend = 1 , yend = .25), colour = "black", size = .5) +
	geom_polygon(data = tempR , aes(x = x , y = y*.25), alpha = 0.15, fill = "#b10f2f") +
	geom_polygon(data = tempD , aes(x = x , y = -y*.25), alpha = 0.15, fill = "#000069") +
	ggtheme_personalized +
	theme(legend.position="right",
		panel.grid.major.y=element_blank(),
		legend.title=element_blank(),
		legend.key = element_rect(colour = "white",fill = "white"),
		legend.text = element_text(size=10,color="grey50"),
		axis.title.y = element_blank(),
		axis.text.y = element_blank(),
		plot.margin = unit(c(0.,0.,0.,0.), "cm"),
		axis.title.x = element_blank(),
		axis.text.x = element_blank()) +
	xlim(c(-.06,1.05)) + ylim(c(-1.3,1.5)) + guides(size = "none", alpha="none" , density="none") +
	scale_colour_manual(breaks = c("R","D"), values = c("#000069","#b10f2f"), labels = c("Republican","Democrat")) +
	annotate("text",x = 0, y = -.5 , label = "Votes 100%\n with Statewide Constituents", colour = "grey30" , size = 4 , hjust = .5) +
	annotate("text",x = 1, y = -.5 , label = "Votes 100%\n with Poor Constituents", colour = "grey30" , size = 4 , hjust = .5) +
	geom_segment(aes(x = weighted.mean(ggdataR$high,ggdataR$N) , y = 0 , xend = weighted.mean(ggdataR$high,ggdataR$N) , yend = 0.5), colour = "#b10f2f", size = 0.5) +
	geom_segment(aes(x = weighted.mean(ggdataD$high,ggdataD$N), y = -.5 , xend = weighted.mean(ggdataD$high,ggdataD$N) , yend = 0), colour = "#000069", size = 0.5) +
	annotate("text", x = 1.0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*ggdata$high[ggdata$party=="D"])," Votes"), size = 5, colour = "#000069", hjust = 1)+
	annotate("text", x = 0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*(1-ggdata$high[ggdata$party=="D"]))," Votes"), size = 5, colour = "#000069", hjust = 0)+
	annotate("text", x = 1.0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*ggdata$high[ggdata$party=="R"]),"  Votes"), size = 5, colour = "#b10f2f", hjust = 1)+
	annotate("text", x = 0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*(1-ggdata$high[ggdata$party=="R"])),"  Votes"), size = 5, colour = "#b10f2f", hjust = 0)+
	annotate("text", x = 0, y = 0, label = "Republicans", size = 4, colour = "#b10f2f", hjust = 1.1 , vjust=-.4)+
	annotate("text", x = 0, y = 0, label = "Democrats", size = 4, colour = "#000069", hjust = 1.1 , vjust=1.2)+
	geom_segment(aes(x = .0, y = 0 , xend = 1.0 , yend = 0), colour = "black", size = 1.5)
dev.off()




ggdata <- df
ggdata$Partisan <- NA
ggdata$Partisan[ggdata$party=="D"] <- ggdata$Dem[ggdata$party=="D"]
ggdata$Partisan[ggdata$party=="R"] <- ggdata$Rep[ggdata$party=="R"]

ggdata <- ddply(
	subset(ggdata , Q5 > 0.5 & Partisan < 0.5 | Q5 < 0.5 & Partisan > 0.5),
	.(senator,party) , function(x) out = data.frame(
		high = mean(x$Q5 > 0.5 & x$vote == 1 | x$Q5 < 0.5 & x$vote == 0 ),
		N = as.numeric(length(x$Q5))
))

set.seed(1234)
ggdata$high2 <- ggdata$high - runif(nrow(ggdata))/100 + .005
ggdata$high3 <- ggdata$high2
ggdata$high2[ggdata$party=="D"] <- 2
ggdata$high3[ggdata$party=="R"] <- 2
ggdataR <- subset(ggdata , N > 0 & party=="R")
ggdataD <- subset(ggdata , N > 0 & party=="D")

tempR <- density(ggdataR$high)
tempR <- data.frame(x = tempR$x , y = tempR$y)
tempR$y[tempR$x > 1] <- 0
tempR$y[tempR$x < 0] <- 0
tempD <- density(ggdataD$high)
tempD <- data.frame(x = tempD$x , y = tempD$y)
tempD$y[tempD$x < 0] <- 0
tempD$y[tempD$x > 1] <- 0

pdf("Figures for Paper/Figure_12a_congruence_by_senator_when_differing_partisan_wealthy.pdf",height=3,width=10.5,onefile=FALSE)
ggplot(data = subset(ggdata , N > 0)) +
 	geom_point(aes(x = high2 , y=.125*N/25, size = N/15), colour = "#b10f2f",fill = "#b10f2f",pch=24,alpha=0.5) +
 	geom_point(aes(x = high3 , y=-.125*N/25, size = N/15), colour = "#000069",fill = "#000069",pch=25,alpha=0.5) +
	geom_segment(aes(x = .5, y = -.25 , xend = .5 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 0, y = -.25 , xend = 0 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 1, y = -.25 , xend = 1 , yend = .25), colour = "black", size = .5) +
	geom_polygon(data = tempR , aes(x = x , y = y*.3), alpha = 0.15, fill = "#b10f2f") +
	geom_polygon(data = tempD , aes(x = x , y = -y*.3), alpha = 0.15, fill = "#000069") +
	ggtheme_personalized +
	theme(legend.position="right",
		panel.grid.major.y=element_blank(),
		legend.title=element_blank(),
		legend.key = element_rect(colour = "white",fill = "white"),
		legend.text = element_text(size=10,color="grey50"),
		axis.title.y = element_blank(),
		axis.text.y = element_blank(),
		plot.margin = unit(c(0.,0.,0.,0.), "cm"),
		axis.title.x = element_blank(),
		axis.text.x = element_blank()) +
	xlim(c(-.06,1.05)) + ylim(c(-1.3,1.5)) + guides(size = "none", alpha="none" , density="none") +
	scale_colour_manual(breaks = c("R","D"), values = c("#000069","#b10f2f"), labels = c("Republican","Democrat")) +
	annotate("text",x = 0, y = -.5 , label = "Votes 100%\n with Partisan Constituents", colour = "grey30" , size = 4 , hjust = .5) +
	annotate("text",x = 1, y = -.5 , label = "Votes 100%\n with Rich Constituents", colour = "grey30" , size = 4 , hjust = .5) +
	geom_segment(aes(x = weighted.mean(ggdataR$high,ggdataR$N) , y = 0 , xend = weighted.mean(ggdataR$high,ggdataR$N) , yend = 0.5), colour = "#b10f2f", size = 0.5) +
	geom_segment(aes(x = weighted.mean(ggdataD$high,ggdataD$N), y = -.5 , xend = weighted.mean(ggdataD$high,ggdataD$N) , yend = 0), colour = "#000069", size = 0.5) +
	annotate("text", x = 1.0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*ggdata$high[ggdata$party=="D"])," Votes"), size = 5, colour = "#000069", hjust = 1)+
	annotate("text", x = 0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*(1-ggdata$high[ggdata$party=="D"]))," Votes"), size = 5, colour = "#000069", hjust = 0)+
	annotate("text", x = 1.0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*ggdata$high[ggdata$party=="R"]),"  Votes"), size = 5, colour = "#b10f2f", hjust = 1)+
	annotate("text", x = 0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*(1-ggdata$high[ggdata$party=="R"])),"  Votes"), size = 5, colour = "#b10f2f", hjust = 0)+
	annotate("text", x = 0, y = 0, label = "Republicans", size = 4, colour = "#b10f2f", hjust = 1.1 , vjust=-.4)+
	annotate("text", x = 0, y = 0, label = "Democrats", size = 4, colour = "#000069", hjust = 1.1 , vjust=1.2)+
	geom_segment(aes(x = .0, y = 0 , xend = 1.0 , yend = 0), colour = "black", size = 1.5)
dev.off()


ggdata <- df
ggdata$Partisan <- NA
ggdata$Partisan[ggdata$party=="D"] <- ggdata$Dem[ggdata$party=="D"]
ggdata$Partisan[ggdata$party=="R"] <- ggdata$Rep[ggdata$party=="R"]

ggdata <- ddply(
	subset(ggdata , Q1 > 0.5 & Partisan < 0.5 | Q1 < 0.5 & Partisan > 0.5),
	.(senator,party) , function(x) out = data.frame(
		high = mean(x$Q1 > 0.5 & x$vote == 1 | x$Q1 < 0.5 & x$vote == 0 ),
		N = as.numeric(length(x$Q5))
))

set.seed(1234)
ggdata$high2 <- ggdata$high - runif(nrow(ggdata))/100 + .005
ggdata$high3 <- ggdata$high2
ggdata$high2[ggdata$party=="D"] <- 2
ggdata$high3[ggdata$party=="R"] <- 2
ggdataR <- subset(ggdata , N > 0 & party=="R")
ggdataD <- subset(ggdata , N > 0 & party=="D")

tempR <- density(ggdataR$high)
tempR <- data.frame(x = tempR$x , y = tempR$y)
tempR$y[tempR$x > 1] <- 0
tempR$y[tempR$x < 0] <- 0
tempD <- density(ggdataD$high)
tempD <- data.frame(x = tempD$x , y = tempD$y)
tempD$y[tempD$x < 0] <- 0
tempD$y[tempD$x > 1] <- 0

pdf("Figures for Paper/Figure_12b_congruence_by_senator_when_differing_partisan_poor.pdf",height=3,width=10.5,onefile=FALSE)
ggplot(data = subset(ggdata , N > 0)) +
 	geom_point(aes(x = high2 , y=.125*N/30, size = N/15), colour = "#b10f2f",fill = "#b10f2f",pch=24,alpha=0.5) +
 	geom_point(aes(x = high3 , y=-.125*N/30, size = N/15), colour = "#000069",fill = "#000069",pch=25,alpha=0.5) +
	geom_segment(aes(x = .5, y = -.25 , xend = .5 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 0, y = -.25 , xend = 0 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 1, y = -.25 , xend = 1 , yend = .25), colour = "black", size = .5) +
	geom_polygon(data = tempR , aes(x = x , y = y*.3), alpha = 0.15, fill = "#b10f2f") +
	geom_polygon(data = tempD , aes(x = x , y = -y*.3), alpha = 0.15, fill = "#000069") +
	ggtheme_personalized +
	theme(legend.position="right",
		panel.grid.major.y=element_blank(),
		legend.title=element_blank(),
		legend.key = element_rect(colour = "white",fill = "white"),
		legend.text = element_text(size=10,color="grey50"),
		axis.title.y = element_blank(),
		axis.text.y = element_blank(),
		plot.margin = unit(c(0.,0.,0.,0.), "cm"),
		axis.title.x = element_blank(),
		axis.text.x = element_blank()) +
	xlim(c(-.06,1.05)) + ylim(c(-1.3,1.5)) + guides(size = "none", alpha="none" , density="none") +
	scale_colour_manual(breaks = c("R","D"), values = c("#000069","#b10f2f"), labels = c("Republican","Democrat")) +
	annotate("text",x = 0, y = -.5 , label = "Votes 100%\n with Partisan Constituents", colour = "grey30" , size = 4 , hjust = .5) +
	annotate("text",x = 1, y = -.5 , label = "Votes 100%\n with Poor Constituents", colour = "grey30" , size = 4 , hjust = .5) +
	geom_segment(aes(x = weighted.mean(ggdataR$high,ggdataR$N) , y = 0 , xend = weighted.mean(ggdataR$high,ggdataR$N) , yend = 0.5), colour = "#b10f2f", size = 0.5) +
	geom_segment(aes(x = weighted.mean(ggdataD$high,ggdataD$N), y = -.5 , xend = weighted.mean(ggdataD$high,ggdataD$N) , yend = 0), colour = "#000069", size = 0.5) +
	annotate("text", x = 1.0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*ggdata$high[ggdata$party=="D"])," Votes"), size = 5, colour = "#000069", hjust = 1)+
	annotate("text", x = 0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*(1-ggdata$high[ggdata$party=="D"]))," Votes"), size = 5, colour = "#000069", hjust = 0)+
	annotate("text", x = 1.0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*ggdata$high[ggdata$party=="R"]),"  Votes"), size = 5, colour = "#b10f2f", hjust = 1)+
	annotate("text", x = 0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*(1-ggdata$high[ggdata$party=="R"])),"  Votes"), size = 5, colour = "#b10f2f", hjust = 0)+
	annotate("text", x = 0, y = 0, label = "Republicans", size = 4, colour = "#b10f2f", hjust = 1.1 , vjust=-.4)+
	annotate("text", x = 0, y = 0, label = "Democrats", size = 4, colour = "#000069", hjust = 1.1 , vjust=1.2)+
	geom_segment(aes(x = .0, y = 0 , xend = 1.0 , yend = 0), colour = "black", size = 1.5)
dev.off()




ggdata <- df
ggdata$Partisan <- NA
ggdata$Partisan[ggdata$party=="D"] <- ggdata$Dem[ggdata$party=="D"]
ggdata$Partisan[ggdata$party=="R"] <- ggdata$Rep[ggdata$party=="R"]

ggdata <- ddply(subset(ggdata , Q5 > 0.5 & Q1 < 0.5 & Partisan < 0.5 | Q5 < 0.5 & Q1 > 0.5 & Partisan > 0.5) , .(senator,party) , function(x) out = data.frame(
	high = mean(x$Q5 > 0.5 & x$vote == 1 | x$Q5 < 0.5 & x$vote == 0 ),
	low = mean(x$Q1 > 0.5 & x$vote == 1 | x$Q1 < 0.5 & x$vote == 0 ),
	N = as.numeric(length(x$Q5))
))

set.seed(1234)
ggdata$high2 <- ggdata$high - runif(nrow(ggdata))/100 + .005
ggdata$high3 <- ggdata$high2
ggdata$high2[ggdata$party=="D"] <- 2
ggdata$high3[ggdata$party=="R"] <- 2
ggdataR <- subset(ggdata , N > 0 & party=="R")
ggdataD <- subset(ggdata , N > 0 & party=="D")

tempR <- density(ggdataR$high)
tempR <- data.frame(x = tempR$x , y = tempR$y)
tempR$y[tempR$x > 1] <- 0
tempR$y[tempR$x < 0] <- 0
tempD <- density(ggdataD$high)
tempD <- data.frame(x = tempD$x , y = tempD$y)
tempD$y[tempD$x < 0] <- 0
tempD$y[tempD$x > 1] <- 0


pdf("Figures for Paper/Figure_13a_congruence_by_senator_when_Q5_differs_Q1_and_Party.pdf",height=3,width=10.5,onefile=FALSE)
ggplot(data = subset(ggdata , N > 0)) +
 	geom_point(aes(x = high2 , y=.125*N/25, size = N/15), colour = "#b10f2f",fill = "#b10f2f",pch=24,alpha=0.5) +
 	geom_point(aes(x = high3 , y=-.125*N/25, size = N/15), colour = "#000069",fill = "#000069",pch=25,alpha=0.5) +
	geom_segment(aes(x = .0, y = 0 , xend = 1.0 , yend = 0), colour = "black", size = 1.5) +
	geom_segment(aes(x = .5, y = -.25 , xend = .5 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 0, y = -.25 , xend = 0 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 1, y = -.25 , xend = 1 , yend = .25), colour = "black", size = .5) +
	geom_polygon(data = tempR , aes(x = x , y = y*.225), alpha = 0.15, fill = "#b10f2f") +
	geom_polygon(data = tempD , aes(x = x , y = -y*.225), alpha = 0.15, fill = "#000069") +
	ggtheme_personalized +
	theme(legend.position="right",
		panel.grid.major.y=element_blank(),
		legend.title=element_blank(),
		legend.key = element_rect(colour = "white",fill = "white"),
		legend.text = element_text(size=10,color="grey50"),
		axis.title.y = element_blank(),
		axis.text.y = element_blank(),
		plot.margin = unit(c(0.,0.,0.,0.), "cm"),
		axis.title.x = element_blank(),
		axis.text.x = element_blank()) +
	xlim(c(-.06,1.05)) + ylim(c(-1.3,1.5)) + guides(size = "none", alpha="none" , density="none") +
	scale_colour_manual(breaks = c("R","D"), values = c("#000069","#b10f2f"), labels = c("Republican","Democrat")) +
	annotate("text",x = 0, y = -.5 , label = "Votes 100%\n with Poor and Party", colour = "grey30" , size = 4 , hjust = .5) +
	annotate("text",x = 1, y = -.5 , label = "Votes 100%\n with Rich", colour = "grey30" , size = 4 , hjust = .5) +
	geom_segment(aes(x = weighted.mean(ggdataR$high,ggdataR$N) , y = 0 , xend = weighted.mean(ggdataR$high,ggdataR$N) , yend = 0.5), colour = "#b10f2f", size = 0.5) +
	geom_segment(aes(x = weighted.mean(ggdataD$high,ggdataD$N), y = -.5 , xend = weighted.mean(ggdataD$high,ggdataD$N) , yend = 0), colour = "#000069", size = 0.5) +
	annotate("text", x = 1.0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*ggdata$high[ggdata$party=="D"])," Votes"), size = 5, colour = "#000069", hjust = 1)+
	annotate("text", x = 0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*(1-ggdata$high[ggdata$party=="D"]))," Votes"), size = 5, colour = "#000069", hjust = 0)+
	annotate("text", x = 1.0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*ggdata$high[ggdata$party=="R"]),"  Votes"), size = 5, colour = "#b10f2f", hjust = 1)+
	annotate("text", x = 0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*(1-ggdata$high[ggdata$party=="R"])),"  Votes"), size = 5, colour = "#b10f2f", hjust = 0)+
	annotate("text", x = 0, y = 0, label = "Republicans", size = 4, colour = "#b10f2f", hjust = 1.1 , vjust=-.4)+
	annotate("text", x = 0, y = 0, label = "Democrats", size = 4, colour = "#000069", hjust = 1.1 , vjust=1.2)+
	geom_segment(aes(x = .0, y = 0 , xend = 1.0 , yend = 0), colour = "black", size = 1.5)
dev.off()


### Congruence by income group when PARTY MEDIAN takes side with RICH AGAINST POOR (Figure 18)
ggdata <- df
ggdata$Partisan <- NA
ggdata$Partisan[ggdata$party=="D"] <- ggdata$Dem[ggdata$party=="D"]
ggdata$Partisan[ggdata$party=="R"] <- ggdata$Rep[ggdata$party=="R"]


ggdata <- ddply(subset(ggdata , Q5 > 0.5 & Q1 < 0.5 & Partisan > 0.5 | Q5 < 0.5 & Q1 > 0.5 & Partisan < 0.5) , .(senator,party) , function(x) out = data.frame(
	high = mean(x$Q5 > 0.5 & x$vote == 1 | x$Q5 < 0.5 & x$vote == 0 ),
	low = mean(x$Q1 > 0.5 & x$vote == 1 | x$Q1 < 0.5 & x$vote == 0 ),
	N = as.numeric(length(x$Q5))
))

set.seed(1234)
ggdata$high2 <- ggdata$high - runif(nrow(ggdata))/100 + .005
ggdata$high3 <- ggdata$high2
ggdata$high2[ggdata$party=="D"] <- 2
ggdata$high3[ggdata$party=="R"] <- 2
ggdataR <- subset(ggdata , N > 0 & party=="R")
ggdataD <- subset(ggdata , N > 0 & party=="D")



tempR <- density(ggdataR$high)
tempR <- data.frame(x = tempR$x , y = tempR$y)
tempR$y[tempR$x > 1] <- 0
tempR$y[tempR$x < 0] <- 0
tempD <- density(ggdataD$high)
tempD <- data.frame(x = tempD$x , y = tempD$y)
tempD$y[tempD$x < 0] <- 0
tempD$y[tempD$x > 1] <- 0


pdf("Figures for Paper/Figure_13b_congruence_by_senator_when_Q5_and_Party_differs_Q1.pdf",height=3,width=10.5,onefile=FALSE)
ggplot(data = subset(ggdata , N > 0)) +
 	geom_point(aes(x = high2 , y=.125*N/25, size = N/15), colour = "#b10f2f",fill = "#b10f2f",pch=24,alpha=0.5) +
 	geom_point(aes(x = high3 , y=-.125*N/25, size = N/15), colour = "#000069",fill = "#000069",pch=25,alpha=0.5) +
	geom_segment(aes(x = .5, y = -.25 , xend = .5 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 0, y = -.25 , xend = 0 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 1, y = -.25 , xend = 1 , yend = .25), colour = "black", size = .5) +
	geom_polygon(data = tempR , aes(x = x , y = y*.10), alpha = 0.15, fill = "#b10f2f") +
	geom_polygon(data = tempD , aes(x = x , y = -y*.05), alpha = 0.15, fill = "#000069") +
	ggtheme_personalized +
	theme(legend.position="right",
		panel.grid.major.y=element_blank(),
		legend.title=element_blank(),
		legend.key = element_rect(colour = "white",fill = "white"),
		legend.text = element_text(size=10,color="grey50"),
		axis.title.y = element_blank(),
		axis.text.y = element_blank(),
		plot.margin = unit(c(0.,0.,0.,0.), "cm"),
		axis.title.x = element_blank(),
		axis.text.x = element_blank()) +
	xlim(c(-.06,1.05)) + ylim(c(-1.3,1.5)) + guides(size = "none", alpha="none" , density="none") +
	scale_colour_manual(breaks = c("R","D"), values = c("#000069","#b10f2f"), labels = c("Republican","Democrat")) +
	annotate("text",x = 0, y = -.5 , label = "Votes 100%\n with Poor", colour = "grey30" , size = 4 , hjust = .5) +
	annotate("text",x = 1, y = -.5 , label = "Votes 100%\n with Rich and Party", colour = "grey30" , size = 4 , hjust = .5) +
	geom_segment(aes(x = weighted.mean(ggdataR$high,ggdataR$N) , y = 0 , xend = weighted.mean(ggdataR$high,ggdataR$N) , yend = 0.5), colour = "#b10f2f", size = 0.5) +
	geom_segment(aes(x = weighted.mean(ggdataD$high,ggdataD$N), y = -.5 , xend = weighted.mean(ggdataD$high,ggdataD$N) , yend = 0), colour = "#000069", size = 0.5) +
	annotate("text", x = 1.0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*ggdata$high[ggdata$party=="D"])," Votes"), size = 5, colour = "#000069", hjust = 1)+
	annotate("text", x = 0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*(1-ggdata$high[ggdata$party=="D"]))," Votes"), size = 5, colour = "#000069", hjust = 0)+
	annotate("text", x = 1.0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*ggdata$high[ggdata$party=="R"]),"  Votes"), size = 5, colour = "#b10f2f", hjust = 1)+
	annotate("text", x = 0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*(1-ggdata$high[ggdata$party=="R"])),"  Votes"), size = 5, colour = "#b10f2f", hjust = 0)+
	annotate("text", x = 0, y = 0, label = "Republicans", size = 4, colour = "#b10f2f", hjust = 1.1 , vjust=-.4)+
	annotate("text", x = 0, y = 0, label = "Democrats", size = 4, colour = "#000069", hjust = 1.1 , vjust=1.2)+
	geom_segment(aes(x = .0, y = 0 , xend = 1.0 , yend = 0), colour = "black", size = 1.5)
dev.off()

ggdata <- df
ggdata$PartisanQ1 <- ggdata$PartisanQ5 <- NA
ggdata$PartisanQ1[ggdata$party=="D"] <- ggdata$Q1D[ggdata$party=="D"]
ggdata$PartisanQ5[ggdata$party=="D"] <- ggdata$Q5D[ggdata$party=="D"]
ggdata$PartisanQ1[ggdata$party=="R"] <- ggdata$Q1R[ggdata$party=="R"]
ggdata$PartisanQ5[ggdata$party=="R"] <- ggdata$Q5R[ggdata$party=="R"]


ggdata <- ddply(subset(ggdata , PartisanQ5 > 0.5 & PartisanQ1 < 0.5 | PartisanQ5 < 0.5 & PartisanQ1 > 0.5) , .(senator,party) , function(x) out = data.frame(
	high = mean(x$PartisanQ5 > 0.5 & x$vote == 1 | x$PartisanQ5 < 0.5 & x$vote == 0 ),
	N = as.numeric(length(x$Q5))
))

set.seed(1234)
ggdata$high2 <- ggdata$high - runif(nrow(ggdata))/100 + .005
ggdata$high3 <- ggdata$high2
ggdata$high2[ggdata$party=="D"] <- 2
ggdata$high3[ggdata$party=="R"] <- 2
ggdataR <- subset(ggdata , N > 0 & party=="R")
ggdataD <- subset(ggdata , N > 0 & party=="D")


tempR <- density(ggdataR$high)
tempR <- data.frame(x = tempR$x , y = tempR$y)
tempR$y[tempR$x > 1] <- 0
tempR$y[tempR$x < 0] <- 0
tempD <- density(ggdataD$high)
tempD <- data.frame(x = tempD$x , y = tempD$y)
tempD$y[tempD$x < 0] <- 0
tempD$y[tempD$x > 1] <- 0


pdf("Figures for Paper/Figure_14_congruence_by_senator_when_Q5_and_Q1_differ_within_party.pdf",height=3,width=10.5,onefile=FALSE)
ggplot(data = subset(ggdata , N > 0)) +
 	geom_point(aes(x = high2 , y=.125*N/15, size = N/15), colour = "#b10f2f",fill = "#b10f2f",pch=24,alpha=0.5) +
 	geom_point(aes(x = high3 , y=-.125*N/15, size = N/15), colour = "#000069",fill = "#000069",pch=25,alpha=0.5) +
	geom_segment(aes(x = .5, y = -.25 , xend = .5 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 0, y = -.25 , xend = 0 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 1, y = -.25 , xend = 1 , yend = .25), colour = "black", size = .5) +
	geom_polygon(data = tempR , aes(x = x , y = y*.125), alpha = 0.15, fill = "#b10f2f") +
	geom_polygon(data = tempD , aes(x = x , y = -y*.225), alpha = 0.15, fill = "#000069") +
	ggtheme_personalized +
	theme(legend.position="right",
		panel.grid.major.y=element_blank(),
		legend.title=element_blank(),
		legend.key = element_rect(colour = "white",fill = "white"),
		legend.text = element_text(size=10,color="grey50"),
		axis.title.y = element_blank(),
		axis.text.y = element_blank(),
		plot.margin = unit(c(0.,0.,0.,0.), "cm"),
		axis.title.x = element_blank(),
		axis.text.x = element_blank()) +
	xlim(c(-.06,1.05)) + ylim(c(-1.3,1.5)) + guides(size = "none", alpha="none" , density="none") +
	scale_colour_manual(breaks = c("R","D"), values = c("#000069","#b10f2f"), labels = c("Republican","Democrat")) +
	annotate("text",x = 0, y = -.5 , label = "Votes 100%\n with Poor Partisans", colour = "grey30" , size = 4 , hjust = .5) +
	annotate("text",x = 1, y = -.5 , label = "Votes 100%\n with Rich Partisans", colour = "grey30" , size = 4 , hjust = .5) +
	geom_segment(aes(x = weighted.mean(ggdataR$high,ggdataR$N) , y = 0 , xend = weighted.mean(ggdataR$high,ggdataR$N) , yend = 0.5), colour = "#b10f2f", size = 0.5) +
	geom_segment(aes(x = weighted.mean(ggdataD$high,ggdataD$N), y = -.5 , xend = weighted.mean(ggdataD$high,ggdataD$N) , yend = 0), colour = "#000069", size = 0.5) +
	annotate("text", x = 1.0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*ggdata$high[ggdata$party=="D"])," Votes"), size = 5, colour = "#000069", hjust = 1)+
	annotate("text", x = 0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*(1-ggdata$high[ggdata$party=="D"]))," Votes"), size = 5, colour = "#000069", hjust = 0)+
	annotate("text", x = 1.0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*ggdata$high[ggdata$party=="R"]),"  Votes"), size = 5, colour = "#b10f2f", hjust = 1)+
	annotate("text", x = 0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*(1-ggdata$high[ggdata$party=="R"])),"  Votes"), size = 5, colour = "#b10f2f", hjust = 0)+
	annotate("text", x = 0, y = 0, label = "Republicans", size = 4, colour = "#b10f2f", hjust = 1.1 , vjust=-.4)+
	annotate("text", x = 0, y = 0, label = "Democrats", size = 4, colour = "#000069", hjust = 1.1 , vjust=1.2)+
	geom_segment(aes(x = .0, y = 0 , xend = 1.0 , yend = 0), colour = "black", size = 1.5)
dev.off()



ggdata0 <- ddply(df , .(issue) , function(x) out = data.frame(
	medianRep = mean(x$vote[x$party=="R"]),
	medianDem = mean(x$vote[x$party=="D"])))
table(ggdata0$medianDem > 0.5 , ggdata0$medianRep > 0.5)

ggdata1 <- join(df , ggdata0)

ggdata1$Partisan <- ggdata1$PartisanElite <- NA
ggdata1$Partisan[ggdata1$party=="D"] <- ggdata1$Dem[ggdata1$party=="D"]
ggdata1$Partisan[ggdata1$party=="R"] <- ggdata1$Rep[ggdata1$party=="R"]
ggdata1$PartisanElite[ggdata1$party=="D"] <- ggdata1$medianDem[ggdata1$party=="D"]
ggdata1$PartisanElite[ggdata1$party=="R"] <- ggdata1$medianRep[ggdata1$party=="R"]

ggdata <- ddply(subset(ggdata1 , PartisanElite > 0.5 & Partisan < 0.5 | PartisanElite < 0.5 & Partisan > 0.5) ,
	.(senator,party) , function(x) out = data.frame(
	high = mean(x$PartisanElite > 0.5 & x$vote == 1 | x$PartisanElite < 0.5 & x$vote == 0 ),
	N = as.numeric(length(x$Q5))
))

set.seed(1234)
ggdata$high2 <- ggdata$high - runif(nrow(ggdata))/100 + .005
ggdata$high3 <- ggdata$high2
ggdata$high2[ggdata$party=="D"] <- 2
ggdata$high3[ggdata$party=="R"] <- 2
ggdataR <- subset(ggdata , N > 0 & party=="R")
ggdataD <- subset(ggdata , N > 0 & party=="D")


tempR <- density(ggdataR$high)
tempR <- data.frame(x = tempR$x , y = tempR$y)
tempR$y[tempR$x > 1] <- 0
tempR$y[tempR$x < 0] <- 0
tempD <- density(ggdataD$high)
tempD <- data.frame(x = tempD$x , y = tempD$y)
tempD$y[tempD$x < 0] <- 0
tempD$y[tempD$x > 1] <- 0

pdf("Figures for Paper/Figure_15b_congruence_elites_partisans.pdf",height=3,width=10.5,onefile=FALSE)
ggplot(data = subset(ggdata , N > 0)) +
 	geom_point(aes(x = high2 , y=.125*N/25, size = N/15), colour = "#b10f2f",fill = "#b10f2f",pch=24,alpha=0.5) +
 	geom_point(aes(x = high3 , y=-.125*N/25, size = N/15), colour = "#000069",fill = "#000069",pch=25,alpha=0.5) +
	geom_segment(aes(x = .5, y = -.25 , xend = .5 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 0, y = -.25 , xend = 0 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 1, y = -.25 , xend = 1 , yend = .25), colour = "black", size = .5) +
	geom_polygon(data = tempR , aes(x = x , y = y*.2), alpha = 0.15, fill = "#b10f2f") +
	geom_polygon(data = tempD , aes(x = x , y = -y*.2), alpha = 0.15, fill = "#000069") +
	ggtheme_personalized +
	theme(legend.position="right",
		panel.grid.major.y=element_blank(),
		legend.title=element_blank(),
		legend.key = element_rect(colour = "white",fill = "white"),
		legend.text = element_text(size=10,color="grey50"),
		axis.title.y = element_blank(),
		axis.text.y = element_blank(),
		plot.margin = unit(c(0.,0.,0.,0.), "cm"),
		axis.title.x = element_blank(),
		axis.text.x = element_blank()) +
	xlim(c(-.06,1.05)) + ylim(c(-1.1,1.1)) + guides(size = "none", alpha="none" , density="none") +
	scale_colour_manual(breaks = c("R","D"), values = c("#000069","#b10f2f"), labels = c("Republican","Democrat")) +
	annotate("text",x = 0, y = -.5 , label = "Votes 100%\n with Partisan\nConstituents", colour = "grey30" , size = 4 , hjust = .5) +
	annotate("text",x = 1, y = -.5 , label = "Votes 100%\n with Partisan\nSenate Peers", colour = "grey30" , size = 4 , hjust = .5) +
	geom_segment(aes(x = weighted.mean(ggdataR$high,ggdataR$N) , y = 0 , xend = weighted.mean(ggdataR$high,ggdataR$N) , yend = 0.5), colour = "#b10f2f", size = 0.5) +
	geom_segment(aes(x = weighted.mean(ggdataD$high,ggdataD$N), y = -.5 , xend = weighted.mean(ggdataD$high,ggdataD$N) , yend = 0), colour = "#000069", size = 0.5) +
	annotate("text", x = 1.0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*ggdata$high[ggdata$party=="D"])," Votes"), size = 5, colour = "#000069", hjust = 1)+
	annotate("text", x = 0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*(1-ggdata$high[ggdata$party=="D"]))," Votes"), size = 5, colour = "#000069", hjust = 0)+
	annotate("text", x = 1.0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*ggdata$high[ggdata$party=="R"]),"  Votes"), size = 5, colour = "#b10f2f", hjust = 1)+
	annotate("text", x = 0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*(1-ggdata$high[ggdata$party=="R"])),"  Votes"), size = 5, colour = "#b10f2f", hjust = 0)+
	annotate("text", x = 0, y = 0, label = "Republicans", size = 4, colour = "#b10f2f", hjust = 1.1 , vjust=-.4)+
	annotate("text", x = 0, y = 0, label = "Democrats", size = 4, colour = "#000069", hjust = 1.1 , vjust=1.2)+
	geom_segment(aes(x = .0, y = 0 , xend = 1.0 , yend = 0), colour = "black", size = 1.5)
dev.off()



ggdata <- ddply(subset(ggdata1 , PartisanElite > 0.5 & Q5 < 0.5 | PartisanElite < 0.5 & Q5 > 0.5) ,
	.(senator,party) , function(x) out = data.frame(
	high = mean(x$PartisanElite > 0.5 & x$vote == 1 | x$PartisanElite < 0.5 & x$vote == 0 ),
	N = as.numeric(length(x$Q5))
))


set.seed(1234)
ggdata$high2 <- ggdata$high - runif(nrow(ggdata))/100 + .005
ggdata$high3 <- ggdata$high2
ggdata$high2[ggdata$party=="D"] <- 2
ggdata$high3[ggdata$party=="R"] <- 2

ggdataR <- subset(ggdata , N > 0 & party=="R")
ggdataD <- subset(ggdata , N > 0 & party=="D")


tempR <- density(ggdataR$high)
tempR <- data.frame(x = tempR$x , y = tempR$y)
tempR$y[tempR$x > 1] <- 0
tempR$y[tempR$x < 0] <- 0
tempD <- density(ggdataD$high)
tempD <- data.frame(x = tempD$x , y = tempD$y)
tempD$y[tempD$x < 0] <- 0
tempD$y[tempD$x > 1] <- 0


pdf("Figures for Paper/Figure_15a_congruence_elites_rich.pdf",height=3,width=10.5,onefile=FALSE)
ggplot(data = subset(ggdata , N > 0)) +
 	geom_point(aes(x = high2 , y=.125*N/50, size = N/15), colour = "#b10f2f",fill = "#b10f2f",pch=24,alpha=0.5) +
 	geom_point(aes(x = high3 , y=-.125*N/50, size = N/15), colour = "#000069",fill = "#000069",pch=25,alpha=0.5) +
	geom_segment(aes(x = .5, y = -.25 , xend = .5 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 0, y = -.25 , xend = 0 , yend = .25), colour = "black", size = .5) +
	geom_segment(aes(x = 1, y = -.25 , xend = 1 , yend = .25), colour = "black", size = .5) +
	geom_polygon(data = tempR , aes(x = x , y = y*.2), alpha = 0.15, fill = "#b10f2f") +
	geom_polygon(data = tempD , aes(x = x , y = -y*.2), alpha = 0.15, fill = "#000069") +
	ggtheme_personalized +
	theme(legend.position="right",
		panel.grid.major.y=element_blank(),
		legend.title=element_blank(),
		legend.key = element_rect(colour = "white",fill = "white"),
		legend.text = element_text(size=10,color="grey50"),
		axis.title.y = element_blank(),
		axis.text.y = element_blank(),
		plot.margin = unit(c(0.,0.,0.,0.), "cm"),
		axis.title.x = element_blank(),
		axis.text.x = element_blank()) +
	xlim(c(-.06,1.05)) + ylim(c(-1.1,1.1)) + guides(size = "none", alpha="none" , density="none") +
	scale_colour_manual(breaks = c("R","D"), values = c("#000069","#b10f2f"), labels = c("Republican","Democrat")) +
	annotate("text",x = 0, y = -.5 , label = "Votes 100%\n with Rich\nConstituents", colour = "grey30" , size = 4 , hjust = .5) +
	annotate("text",x = 1, y = -.5 , label = "Votes 100%\n with Partisan\nSenate Peers", colour = "grey30" , size = 4 , hjust = .5) +
	geom_segment(aes(x = weighted.mean(ggdataR$high,ggdataR$N) , y = 0 , xend = weighted.mean(ggdataR$high,ggdataR$N) , yend = 0.5), colour = "#b10f2f", size = 0.5) +
	geom_segment(aes(x = weighted.mean(ggdataD$high,ggdataD$N), y = -.5 , xend = weighted.mean(ggdataD$high,ggdataD$N) , yend = 0), colour = "#000069", size = 0.5) +
	annotate("text", x = 1.0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*ggdata$high[ggdata$party=="D"])," Votes"), size = 5, colour = "#000069", hjust = 1)+
	annotate("text", x = 0, y = -1., label = paste0(sum(ggdata$N[ggdata$party=="D"]*(1-ggdata$high[ggdata$party=="D"]))," Votes"), size = 5, colour = "#000069", hjust = 0)+
	annotate("text", x = 1.0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*ggdata$high[ggdata$party=="R"]),"  Votes"), size = 5, colour = "#b10f2f", hjust = 1)+
	annotate("text", x = 0, y = 1., label = paste0(sum(ggdata$N[ggdata$party=="R"]*(1-ggdata$high[ggdata$party=="R"])),"  Votes"), size = 5, colour = "#b10f2f", hjust = 0)+
	annotate("text", x = 0, y = 0, label = "Republicans", size = 4, colour = "#b10f2f", hjust = 1.1 , vjust=-.4)+
	annotate("text", x = 0, y = 0, label = "Democrats", size = 4, colour = "#000069", hjust = 1.1 , vjust=1.2)+
	geom_segment(aes(x = .0, y = 0 , xend = 1.0 , yend = 0), colour = "black", size = 1.5)
dev.off()
